const {
  unDefined,
  nullValue,
  emailver,
  passwordver,
} = require("@/utils/valiData");
const result = require("@/utils/handle");

// 发送验证码校验
// 登录校验
const Vercode = (email) => {
  unDefined([email]);
  nullValue([{ value: email, tips: "请输入邮箱" }]);
  // 校验邮箱格式
  if (!emailver.test(email)) {
    throw new result("邮箱格式错误", 422);
  }
};

// 小程序端：邮箱，验证码登录
const Emailregistration = (email, code) => {
  unDefined([email, code]);
  nullValue([
    { value: email, tips: "请输入邮箱" },
    { value: code, tips: "请输入验证码" },
  ]);
  // 校验邮箱格式
  if (!emailver.test(email)) {
    throw new result("邮箱格式错误", 422);
  }
};

// 小程序端，设置密码
const Uploadpassword = (email, code, password) => {
  unDefined([email, code, password]);
  nullValue([
    { value: email, tips: "请输入邮箱" },
    { value: code, tips: "请输入验证码" },
    { value: password, tips: "请输入密码" },
  ]);
  // 校验邮箱格式
  if (!emailver.test(email)) {
    throw new result("邮箱格式错误", 422);
  }
  // 校验密码格式
  if (!passwordver.test(password)) {
    throw new result("密码长度为6-20位，且必须包含数字和字母", 422);
  }
};

// 登录校验
const Login = (email, password) => {
  unDefined([email, password]);
  nullValue([
    { value: email, tips: "请输入邮箱" },
    { value: password, tips: "请输入密码" },
  ]);
  // 校验邮箱格式
  if (!emailver.test(email)) {
    throw new result("邮箱格式错误", 422);
  }
  // 校验密码格式
  if (!passwordver.test(password)) {
    throw new result("密码长度为6-20位，且必须包含数字和字母", 422);
  }
};

// 编辑个人资料
const Modifytheuser = (
  nickname,
  avatarUrl,
  gender,
  birthday,
  city,
  backdrop
) => {
  unDefined([nickname, avatarUrl, gender, birthday, city, backdrop]);
  nullValue([
    { value: nickname, tips: "请输入昵称" },
    { value: avatarUrl, tips: "请上传头像" },
    { value: gender, tips: "请选择性别" },
    { value: birthday, tips: "请选择出生日期" },
    { value: city, tips: "请选择常居城市" },
    { value: backdrop, tips: "请上传背景图" },
  ]);
};

module.exports = {
  Vercode,
  Emailregistration,
  Uploadpassword,
  Login,
  Modifytheuser,
};
